diff options
Diffstat (limited to 'app/[lng]/test/table-v2/actions.ts')
| -rw-r--r-- | app/[lng]/test/table-v2/actions.ts | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/app/[lng]/test/table-v2/actions.ts b/app/[lng]/test/table-v2/actions.ts index e1737083..f5fd5f66 100644 --- a/app/[lng]/test/table-v2/actions.ts +++ b/app/[lng]/test/table-v2/actions.ts @@ -5,7 +5,7 @@ import { testProducts, testOrders, testCustomers } from "@/db/schema/test-table- import { createTableService } from "@/components/client-table-v2/adapter/create-table-service"; import { DrizzleTableState } from "@/components/client-table-v2/adapter/drizzle-table-adapter"; import { productColumnDefs, OrderWithDetails, ServerColumnMeta } from "./column-defs"; -import { count, eq, desc, sql, asc } from "drizzle-orm"; +import { SQL, count, eq, desc, sql, asc } from "drizzle-orm"; import { TestProduct } from "@/db/schema/test-table-v2"; // ============================================================ @@ -182,6 +182,31 @@ export async function getOrderTableData(tableState: DrizzleTableState): Promise< const limit = pageSize; const offset = pageIndex * pageSize; + // Build ORDER BY clause based on sorting state + const orderByClauses = + tableState.sorting?.reduce<SQL<unknown>[]>((clauses, sort) => { + const columnMap: Record<string, any> = { + id: testOrders.id, + orderNumber: testOrders.orderNumber, + quantity: testOrders.quantity, + unitPrice: testOrders.unitPrice, + totalAmount: testOrders.totalAmount, + status: testOrders.status, + orderedAt: testOrders.orderedAt, + customerName: testCustomers.name, + customerEmail: testCustomers.email, + customerTier: testCustomers.tier, + productName: testProducts.name, + productSku: testProducts.sku, + }; + + const column = columnMap[sort.id]; + if (!column) return clauses; + + clauses.push(sort.desc ? desc(column) : asc(column)); + return clauses; + }, []) ?? []; + // 커스텀 조인 쿼리 작성 const data = await db .select({ @@ -203,7 +228,7 @@ export async function getOrderTableData(tableState: DrizzleTableState): Promise< .from(testOrders) .leftJoin(testCustomers, eq(testOrders.customerId, testCustomers.id)) .leftJoin(testProducts, eq(testOrders.productId, testProducts.id)) - .orderBy(desc(testOrders.orderedAt)) + .orderBy(...(orderByClauses.length > 0 ? orderByClauses : [desc(testOrders.orderedAt)])) .limit(limit) .offset(offset); |
